package com.sk.bilibili.utils;

import com.sk.bilibili.bean.Discuss;
import com.sk.bilibili.bean.Forward;
import com.sk.bilibili.bean.Praise;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author 邵康
 * @date 2021/12/7 18:50
 */
@Component
public class ChangeNumUtils {

    @Autowired
    private VideoSetService videoSetService;
    @Autowired
    private DiscussService discussService;
    @Autowired
    private ThemService themService;
    @Autowired
    private ForwardService forwardService;
    @Autowired
    private NoteService noteService;

    public void changeNum(Integer type,String field,Integer num,Integer id){
        switch (type){
            case 0:
                videoSetService.changeNum(field,num,id);
                break;
            case 1:
                themService.changeNum(field,num,id);
                break;
            case 2:
                noteService.changeNum(field,num,id);
                break;
            case 3:
                discussService.changeNum(field, num, id);
                break;
            case 4:
                break;
            case 5:
                forwardService.changeNum(field,num,id);
                break;
            default:
                throw new RuntimeException("暂不支持的类型:"+type);
        }
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.DiscussService.add'")
    public void handleDiscussAdd(BiliBiliEvent<Discuss> event){
        Discuss discuss = event.getData();
        if(discuss.getType()==3)return;//评论 没有评论数  属性
        changeNum(discuss.getType(),"discussNum",1,discuss.getTargetId());
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.ForwardService.add'")
    public void handleForwardAdd(BiliBiliEvent<Forward> event){
        Forward forward = event.getData();
        changeNum(forward.getType(),"forwardNum",1,forward.getTargetId());
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.PraiseService.switchPraise'")
    public void handleSwitchPraise(BiliBiliEvent<Praise> event){
        Praise praise = event.getData();
        Boolean add = event.getExtra("add");
        changeNum(praise.getType(),"praiseNum",add ? 1 : -1,praise.getTargetId());
    }

}
